import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {AuthService} from "../../common/service/auth/auth.service";
import {Router} from "@angular/router";
import {LoggerService} from "@ngx-toolkit/logger";

@Component({
  selector: 'login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  hidePassword: boolean = true;

  loginForm: FormGroup;

  constructor(
    private formBuilder: FormBuilder,
    private authService: AuthService,
    private router: Router,
    private logger: LoggerService) {

    this.loginForm = formBuilder.group({
      username: ["", Validators.required],
      password: ["", Validators.required],
      remember: false
    });
    this.logger.info(this.loginForm);

    //如果存在token，跳转到nav
    if(authService.token){
      router.navigateByUrl("nav");
    }

  }

  get username(){
    return this.loginForm.get("username");
  }

  get password(){
    return this.loginForm.get("password");
  }

  setRememberValue(remember: boolean){
    this.loginForm.get("remember").setValue(remember);
  }

  login(){
    if(this.loginForm.valid){
      this.authService.saveTokenByLogin(this.loginForm.value);
    }
  }

  ngOnInit() {

  }

}
